package edu.friday.repository.custom.impl;

import edu.friday.repository.custom.SysRoleCustomRepository;
import edu.friday.utils.SqlUtil;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

public class SysRoleCustomRepositoryImpl implements SysRoleCustomRepository {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public int batchInsertRoleMenu(Long[] roleIds, Long[] menuIds) {
        int len = Math.min(roleIds.length,menuIds.length);
        StringBuffer sql = new StringBuffer();
        sql.append("insert into sys_role_menu (role_id,menu_id) values");
        sql.append(SqlUtil.getBatchInsertSqlStr(len,2));
        Query nativeQuery = entityManager.createNativeQuery(sql.toString());
        int paramindex = 1;
        for (int i=0;i<len;i++){
            nativeQuery.setParameter(paramindex++,roleIds[i]);
            nativeQuery.setParameter(paramindex++,menuIds[i]);
        }
        return nativeQuery.executeUpdate();
    }
}
